<?php
    if (!defined('EXPONENT')) exit('');
    global $user;
    if(exponent_users_isLoggedIn())
    {
        $contact_id='';
        global $db;
        
        $template = new template('profilemodule','_invoice_main',$loc);
        if(isset($_REQUEST['search']))
            $search=$_REQUEST['search'];
        else
            $search='';
        if(isset($_REQUEST['order_by']))
            $order=$_REQUEST['order_by'];
        else
            $order='post_date';
        $page=$_REQUEST['page'];
        if(!isset($page))
           $page=1;
        
        //max
        $max=10;
        $start=($page-1)*$max;
        if($page>0)
        $limit="LIMIT {$start},{$max}";
        //orderby
        $order="ORDER BY $order DESC";     
         $where="user_id= ".$user->id." AND name LIKE '%$search%' $order $limit";
         
         $total = $db->countObjects('profilemodule_invoices',"user_id= ".$user->id." AND name LIKE '%$search%'"); 
         $profile_detail = $db->selectObjects('profilemodule_invoices',$where);

        //convert date time

        foreach ($profile_detail as $value)
        {
            $value->date_create=date("d/m/Y", $value->post_date);
            if($value->document_id)
            {
                $value->decode_id=base64_encode('invoice|'.$value->id.'|pdf|'.$value->name.'|application/pdf');
                $value->file_type='pdf';
            }
        }
        $start=$max*$page-($max-1);
        $totalpage=(ceil($total/$max));
        $template->assign('start',$start);
        $template->assign('curent_page', $page);
        $template->assign('total',$totalpage);
        $template->assign('invoices',$profile_detail);
        //function


        $template->register_permissions(array('administrate','configure','create','edit','delete'),$loc);
        $template->output();

    }
?>
